package com.hemaapp.yjnh.activity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import com.hemaapp.hm_FrameWork.HemaNetTask;
import com.hemaapp.hm_FrameWork.result.HemaBaseResult;
import com.hemaapp.yjnh.BaseFragmentActivity;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.adapter.TabViewPagerAdapter;
import com.hemaapp.yjnh.fragment.AttentionListFragment;

/**我的关注
 * @author GuiyouZhang
 * Date：2016年1月10日 下午3:59:52
 * Email:guiyouzhang@163.com
 */
public class AttentionActivity extends BaseFragmentActivity implements OnClickListener{

	@Bind(R.id.title_left_btn)
	ImageButton titleLeftBtn;
	@Bind(R.id.title_right_btn)
	Button titleRightBtn;
	@Bind(R.id.title_text)
	TextView titleText;
	@Bind(R.id.layout_tab)
	TabLayout layoutTab;
	@Bind(R.id.viewpager)
	ViewPager viewpager;

	private TabViewPagerAdapter pagerAdapter;
	List<Fragment> fragments = new ArrayList<>();
	boolean[] isEdits = new boolean[]{false, false, false, false, false, false, false, false};
	private AttentionListFragment liangkuFragment = new AttentionListFragment();
	private AttentionListFragment farmerFragment = new AttentionListFragment();
	private AttentionListFragment leleFragment = new AttentionListFragment();
	private AttentionListFragment zouzouFragment = new AttentionListFragment();
	private AttentionListFragment cunyouFragment = new AttentionListFragment();
//	private AttentionListFragment OrangicFragment = new AttentionListFragment();//不能关注有机商家，因为有机商家就只有一个
	private AttentionListFragment landmarkFragment = new AttentionListFragment();
	private AttentionListFragment techanFragment = new AttentionListFragment();
	private AttentionListFragment nongxuFragment = new AttentionListFragment();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_collection);
		ButterKnife.bind(this);
		super.onCreate(savedInstanceState);
		initDatas();
	}

	private void initDatas() {
		List<String> titles = new ArrayList<>();
		titles.add("粮库");
		titles.add("田家");
		titles.add("乐乐");
		titles.add("走走");
		titles.add("村游");
//		titles.add("有机");
		titles.add("地标");
		titles.add("特产");
		titles.add("农需");
		fragments.add(liangkuFragment);
		fragments.add(farmerFragment);
		fragments.add(leleFragment);
		fragments.add(zouzouFragment);
		fragments.add(cunyouFragment);
//		fragments.add(OrangicFragment);
		fragments.add(landmarkFragment);
		fragments.add(techanFragment);
		fragments.add(nongxuFragment);
		//获取类型	3:关注田家    4:关注集体    11:关注地标商家    12:关注村游商家
		//13:关注乐乐商家    14:关注特产商家    15:关注粮库商家    16:关注走走商家    18:关注农需商家
		liangkuFragment.setKeytype("15");
		farmerFragment.setKeytype("3");
		leleFragment.setKeytype("13");
		zouzouFragment.setKeytype("16");
		cunyouFragment.setKeytype("12");
		//OrangicFragment.setKeytype("2");
		landmarkFragment.setKeytype("11");
		techanFragment.setKeytype("14");
		nongxuFragment.setKeytype("18");

		pagerAdapter = new TabViewPagerAdapter(getSupportFragmentManager(), fragments, titles);
		viewpager.setAdapter(pagerAdapter);
		viewpager.setOffscreenPageLimit(8);
		layoutTab.setTabMode(TabLayout.MODE_SCROLLABLE);
		layoutTab.setupWithViewPager(viewpager);
		layoutTab.setTabsFromPagerAdapter(pagerAdapter);

		viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			@Override
			public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

			}

			@Override
			public void onPageSelected(int position) {
				if (isEdits[position]) {
					titleRightBtn.setText("完成");
				}else {
					titleRightBtn.setText("编辑");
				}
			}

			@Override
			public void onPageScrollStateChanged(int state) {

			}
		});
	}

	@Override
	protected void findView() {
	}

	@Override
	protected void getExras() {
	}

	@Override
	protected void setListener() {
		titleText.setText("我的关注");
		titleRightBtn.setText("编辑");
	}

	@Override
	protected void callBeforeDataBack(HemaNetTask hemaNetTask) {

	}

	@Override
	protected void callAfterDataBack(HemaNetTask hemaNetTask) {

	}

	@Override
	protected void callBackForServerSuccess(HemaNetTask hemaNetTask, HemaBaseResult hemaBaseResult) {

	}

	@Override
	protected void callBackForServerFailed(HemaNetTask hemaNetTask, HemaBaseResult hemaBaseResult) {

	}

	@Override
	protected void callBackForGetDataFailed(HemaNetTask hemaNetTask, int i) {

	}

	@OnClick({R.id.title_left_btn, R.id.title_right_btn})
	public void onClick(View view) {
		switch (view.getId()) {
			case R.id.title_left_btn:
				finish();
				break;
			case R.id.title_right_btn:
				int index = viewpager.getCurrentItem();
				if (((AttentionListFragment)fragments.get(index)).getAdapter() != null) {
					isEdits[index] = !isEdits[index];
					((AttentionListFragment) fragments.get(index)).setEdit(isEdits[index]);
					if (isEdits[index]) {
						titleRightBtn.setText("完成");
					} else {
						titleRightBtn.setText("编辑");
					}
				}
				break;
		}
	}
}
